package top.hcode.hoj.service.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.nutz.lang.Strings;
import org.springframework.stereotype.Component;
import top.hcode.hoj.dao.TyUserDao;
import top.hcode.hoj.pojo.dto.QuerySampleDTO;
import top.hcode.hoj.pojo.entity.org.TyUser;

/**
 * @author orangej
 * @since 2025-01-23
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class AdminTyUserService {
    private final TyUserDao tyUserDao;

    public IPage<TyUser> getTyUserList(QuerySampleDTO querySampleDTO) {
        IPage<TyUser> pager = new Page<>(querySampleDTO.getCurrentPage(), querySampleDTO.getLimit());

        QueryWrapper<TyUser> query = new QueryWrapper<>();
        if (Strings.isNotBlank(querySampleDTO.getKeyword())) {
            query.like("title", querySampleDTO.getKeyword());
        }
        tyUserDao.page(pager, query);
        return pager;
    }

    public TyUser getTyUser(Long id) {
        return tyUserDao.getById(id);
    }

    public TyUser saveTyUser(TyUser tyUser) {
        tyUserDao.saveOrUpdate(tyUser);
        return tyUser;
    }

    public void deleteTyUser(Long id) {
        tyUserDao.removeById(id);
    }
}
